;assignment 4 PDP-8 Instruction Parsing
INCLUDE Irvine32.inc
.data
;==================;
;= PDP-8 Commands =;
;==================;
PDP8_AND BYTE "AND" ,0
PDP8_TAD BYTE "TAD" ,0
PDP8_ISZ BYTE "ISZ" ,0
PDP8_DCA BYTE "DCA" ,0
PDP8_JMS BYTE "JMS" ,0
PDP8_JMP BYTE "JMP" ,0
PDP8_IOT BYTE "IOT" ,0
PDP8_OPR BYTE "OPR" ,0
;==================
opCode WORD ? 
indirectBit WORD ?
pageBit WORD ?
memAddress WORD ?
octalAddress DWORD ?
tempAddress DWORD ?
charIn BYTE ?
input WORD 1000o
.code

main PROC
	mov ax, input ;Input command
	
	call parseOpCode
	call compare 
	call CRLF
	call getIndirectBit
	call CRLF
	call getPageBit
	call CRLF
	call getAddress
	call CRLF
	mov ebx, 2
	call WriteBinB
exit
main ENDP
;==========================================
;== Take the original number, and parse	 ==
;== it into just the command			 ==
;==========================================
parseOpCode PROC

	mov ecx, 3 ;set loop counter
	SHL ax, 4  ;move over the 4 bits to the set 12 bits for pdp-8
	
	opLoop:
		clc					;clear carry flag
		SHL ax,1			;shift the far left bit into the carry flag
		RCL opCode,1		;push the carry flag into the right of the operator variable
	Loop opLoop
ret
parseOpCode ENDP

;==========================================
;== Compare the operator variable to the ==
;== instruction sets and write the name  ==
;== of that instruction on the screen    ==
;==========================================
compare Proc

	.IF opCode == 0o
		mov edx, OFFSET PDP8_AND
		call WriteString
	.ELSEIF opCode == 1o
		mov edx, OFFSET PDP8_TAD
		call WriteString
	.ELSEIF opCode == 2o
		mov edx, OFFSET PDP8_ISZ
		call WriteString
	.ELSEIF opCode == 3o
		mov edx, OFFSET PDP8_DCA
		call WriteString
	.ELSEIF opCode == 4o
		mov edx, OFFSET PDP8_JMS
		call WriteString
	.ELSEIF opCode == 5o
		mov edx, OFFSET PDP8_JMP
		call WriteString
	.ELSEIF opCode == 6o
		mov edx, OFFSET PDP8_IOT
		call WriteString
	.ELSEIF opCode == 7o
		mov edx, OFFSET PDP8_OPR
		call WriteString
	.ENDIF
	
ret
compare ENDP 

;==========================================
;==			Get indirect bit			 ==
;==										 ==
;==========================================
getIndirectBit Proc

	mov ax, input
	clc
	SHL ax, 8
	RCL indirectBit, 1
	mov eax, 0
	mov ax, indirectBit
	call WriteDec
	
ret
getIndirectBit ENDP

;==========================================
;==			Get Page bit		     	 ==
;==										 ==
;==========================================
getPageBit Proc

	mov ax, input
	clc
	SHL ax, 9
	RCL pageBit, 1
	mov eax, 0
	mov ax, pageBit
	call WriteDec

ret
getPageBit ENDP
;==========================================
;==			Get Memory Address	     	 ==
;==										 ==
;==========================================
getAddress Proc

	mov ax, input
	SHL ax, 10
	mov ecx, 7
	
	addressLoop:
		clc
		SHL ax,1			;shift the far left bit into the carry flag
		RCL memAddress,1
	Loop addressLoop
	

ret
getAddress ENDP
;==========================================
;==			Address to Octal             ==
;==										 ==
;==========================================
addressOctal Proc
	
	mov eax, 0
	mov ax,memAddress
	mov tempAddress, eax
	mov ebx, 262144
	div ebx
	mov edx, 1000000
	mul edx
	mov ecx, eax
	div ebx
	sub tempAddress, eax
	
	
	mov eax,tempAddress
	mov ebx, 32768
	div ebx
	mov edx, 100000
	mul edx
	add ecx, eax
	div ebx
	sub tempAddress, eax
	
	mov eax,tempAddress
	mov ebx, 4096
	div ebx
	mov edx, 10000
	mul edx
	add ecx, eax
	div ebx
	sub tempAddress, eax
	
	mov eax,tempAddress
	mov ebx, 512
	div ebx
	mov edx, 1000
	mul edx
	add ecx, eax
	div ebx
	sub tempAddress, eax
	
	mov eax,tempAddress
	mov ebx, 64
	div ebx
	mov edx, 100
	mul edx
	add ecx, eax
	div ebx
	sub tempAddress, eax
	
	mov eax,tempAddress
	mov ebx, 8
	div ebx
	mov edx, 10
	mul edx
	add ecx, eax
	div ebx
	sub tempAddress, eax
	
	add ecx, tempAddress
	mov octalAddress, ecx
	
	mov eax, 0
	mov eax, octalAddress
	call WriteDec	


ret
addressOctal ENDP

END main